package cn.moningxi.controller;

import cn.moningxi.bean.*;
import cn.moningxi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@SessionAttributes(value = "onlineUser", types = String.class)
public class OrderDetailController {
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    CConsigneeService cConsigneeService;
    @Autowired
    OrderMasterService orderMasterService;
    @Autowired
    OShopCartService oShopCartService;
    @Autowired
    CustomerInfomationService customerInfomationService;

    /**
     * 商品页面下订单
     *
     * @param mv
     * @param orderDetail
     * @return
     */
    @RequestMapping("orderApi/settleOrder")
    public ModelAndView settleOrder(ModelAndView mv, OrderDetail orderDetail, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        //添加订单
        orderMasterService.addDetail(orderDetail, onlineUser.getCustomerId());
        List<OrderMaster> orderDetailList = orderMasterService.queryNoPayByCid(onlineUser.getCustomerId());
        mv.addObject("buyGoodsList", orderDetailList);
        //还回地址
        List<CConsignee> cConsignees = cConsigneeService.addrShow(onlineUser);
        mv.addObject("consigneeVoList", cConsignees);
        mv.setViewName("business/home/pay");
        return mv;
    }

    /**
     * 下单页面添加地址
     *
     * @param mv
     * @param consignee
     * @param request
     * @return
     */
    @RequestMapping("orderApi/addConsignee")
    public ModelAndView addConsignee(ModelAndView mv, CConsignee consignee, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        cConsigneeService.addaddr(onlineUser, consignee);
        List<OrderMaster> orderDetailList = orderMasterService.queryNoPayByCid(onlineUser.getCustomerId());
        mv.addObject("buyGoodsList", orderDetailList);
        //还回地址
        List<CConsignee> cConsignees = cConsigneeService.addrShow(onlineUser);
        mv.setViewName("redirect:/orderApi/queryOrderByCId");
        return mv;
    }

    /**
     * 根据用户ID查询未支付的订单，跳转支付页面
     *
     * @param mv
     * @param request
     * @return
     */
    @RequestMapping("/orderApi/queryOrderByCId")
    public ModelAndView queryOrderByCId(ModelAndView mv, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        onlineUser = customerInfomationService.queryUser(onlineUser);
        List<OrderMaster> orderDetailList = orderMasterService.queryNoPayByCid(onlineUser.getCustomerId());
        mv.addObject("buyGoodsList", orderDetailList);
        //还回地址
        List<CConsignee> cConsignees = cConsigneeService.addrShow(onlineUser);
        mv.addObject("consigneeVoList", cConsignees);
        mv.addObject("onlineUser", onlineUser);
        mv.setViewName("business/home/pay");
        return mv;
    }

    /**
     * 购物车下单
     *
     * @param mv
     * @param shopCartIdArr
     * @param amountArr
     * @param request
     * @return
     */
    @RequestMapping("orderApi/settleShopCart")
    public ModelAndView settleShopCart(ModelAndView mv, String shopCartIdArr, String amountArr, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        String[] shopCartId = shopCartIdArr.split(",");
        String[] amount = amountArr.split(",");
        int index = 0;
        //遍历购物车id加入订单
        for (String s : shopCartId) {
            if (!s.equals("")) {
                OShopCart oShopCart = oShopCartService.queryShopCartById(Long.valueOf(s));
                //根据购物车数据设置订单属性
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setGoodsId(oShopCart.getGoodsId());
                orderDetail.setGoodsName(oShopCart.getGoodsName());
                orderDetail.setGoodsPrice(oShopCart.getRealBuyPrice());
                orderDetail.setAverageCost(oShopCart.getShopPrice());
                orderDetail.setGoodsCnt(Integer.valueOf(amount[index++]));
                orderMasterService.addDetail(orderDetail, onlineUser.getCustomerId());
                //删除购物车数据
                oShopCartService.removeShopCart(Integer.valueOf(s));
            }
        }
        mv.setViewName("redirect:/orderApi/queryOrderByCId");
        return mv;
    }

    /**
     * 结算页面，更新商品数量
     *
     * @param mv
     * @param orderId
     * @param amount
     * @return
     */
    @RequestMapping("orderApi/updateOrderCnt")
    public ModelAndView updateOrderCnt(ModelAndView mv, Long orderId, Integer amount) {
        orderDetailService.updateOrderCnt(orderId, amount);
        mv.setViewName("redirect:/orderApi/queryOrderByCId");
        return mv;
    }

    /**
     * 结算页面添加默认地址
     *
     * @param mv
     * @param customerAddrId
     * @param request
     * @return
     */
    @RequestMapping("orderApi/isDefaultAddr")
    public ModelAndView isDefaultAddr(ModelAndView mv, Integer customerAddrId, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        cConsigneeService.isDefaultAddr(customerAddrId, onlineUser);
        mv.setViewName("redirect:/orderApi/queryOrderByCId");
        return mv;
    }
}
